package com.visitor.job;

import com.visitor.bean.Visitor;
import com.visitor.bean.VisitorRecord;
import com.visitor.service.VisitorRecordService;
import com.visitor.service.VisitorService;
import com.visitor.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;


/**
  * @Description(功能描述): 访客过期处理
 *  过期的预约管理将放入预约记录列表里
  **/
@Component
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class VisitorTimeOutJob {
	@Autowired
	private VisitorService visitorService;
	@Autowired
	private VisitorRecordService visitorRecordService;
	@Scheduled(cron = "0 0/1 * * * ?")
	private void configureTasks() {
		List<Visitor>  list=visitorService.findVisitorList("");
		if(list.size()>0){
			for(Visitor v:list){
				Long now=Long.valueOf(DateUtils.getTime2());
				Long appointmentTime=Long.valueOf(v.getAppointmentTime().replaceAll("-","").replaceAll(" ",""));
				if(appointmentTime-now<=0){
					VisitorRecord visitorRecord=new VisitorRecord();
					BeanUtils.copyProperties(v,visitorRecord);
					visitorRecordService.save(visitorRecord);
					visitorService.deleteUserById(Long.valueOf(v.getId()));
				}
			}
		}
	}
}
